
import Head from 'next/head'

<Head>
  <script>
    {
      `(function() {
         var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?e60fb290e204e04c5cb6f79b0ac1e697";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
       })();`
    }
  </script>
</Head>

![LangChain](https://pica.zhimg.com/50/v2-56e8bbb52aa271012541c1fe1ceb11a2_r.gif)



kNN
===


本笔记本演示了如何使用基于kNN的检索器。

主要基于https://github.com/karpathy/randomfun/blob/master/knn_vs_svm.ipynb

```python
from langchain.retrievers import KNNRetriever
from langchain.embeddings import OpenAIEmbeddings

```

使用文本创建新的检索器。[#](#create-new-retriever-with-texts "Permalink to this headline")
-------------------------------------------------------------------------------------------------

```python
retriever = KNNRetriever.from_texts(["foo", "bar", "world", "hello", "foo bar"], OpenAIEmbeddings())

```

使用检索器 Use Retriever[#](#use-retriever "Permalink to this headline")
-------------------------------------------------------------

现在我们可以尝试使用我们的检索器了！

```python
result = retriever.get_relevant_documents("foo")

```

```python
result

```

```python
[Document(page_content='foo', metadata={}),
 Document(page_content='foo bar', metadata={}),
 Document(page_content='hello', metadata={}),
 Document(page_content='bar', metadata={})]

```

